package com.github.caijh.mall.aop;

import java.util.Arrays;

import com.github.caijh.framework.log.util.LogUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.springframework.stereotype.Component;

/**
 * record controller invoke time.
 */
@Aspect
@Component
public class ControllerAspect {

    private final Logger logger = LogUtils.getLogger(getClass());

    @Pointcut("execution(* com.github.caijh.mall.system.controller.*.*(..))")
    private void controllerPointCut() {
        // empty
    }

    @Around("controllerPointCut()")
    public Object doAroundService(ProceedingJoinPoint pjp) throws Throwable {
        long begin = System.nanoTime();
        Object obj = pjp.proceed();
        long end = System.nanoTime();

        String argsString = Arrays.toString(pjp.getArgs());
        logger.info("Controller method：{}，prams：{}，cost time：{} ns，cost：{} ms",
                pjp.getSignature(), argsString, (end - begin), (end - begin) / 1000000);

        return obj;
    }


}
